namespace DotNetNuke.Modules.Admin.ResourceInstaller
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Services.Localization;
    using DotNetNuke.Services.Upgrade;
    using DotNetNuke.UI.Skins;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.IO;
    using System.Web;
    using System.Xml;

    public class ResourceInstaller
    {
        private void DeleteFile(string strFile)
        {
            try
            {
                File.SetAttributes(strFile, FileAttributes.Normal);
                File.Delete(strFile);
            }
            catch (Exception exception1)
            {
                
                
            }
        }

        public void Install()
        {
            this.Install(false, 0, "");
        }

        public void Install(bool status, int indent)
        {
            this.Install(status, indent, "");
        }

        public void Install(bool status, int indent, string type)
        {
            string InstallPath = Globals.ApplicationMapPath + @"\Install";
            if (Directory.Exists(InstallPath))
            {
                foreach (string strFolder in Directory.GetDirectories(InstallPath))
                {
                    foreach (string strFile in Directory.GetFiles(strFolder))
                    {
                        string refStringHelperL0 = type.ToLower();
                        if (refStringHelperL0 == "modules")
                        {
                            this.InstallModules(strFile, status, indent);
                        }
                        else
                        {
                            this.InstallModules(strFile, status, indent);
                            if ((strFile.ToLower().IndexOf(@"\skin\") != -1) && (Path.GetExtension(strFile.ToLower()) == ".zip"))
                            {
                                if (status)
                                {
                                    HtmlUtils.WriteFeedback(HttpContext.Current.Response, indent, "Installing Skin File " + strFile + ":<br>");
                                }
                                SkinController.UploadSkin(Globals.HostMapPath, SkinInfo.RootSkin, Path.GetFileNameWithoutExtension(strFile), strFile);
                                this.DeleteFile(strFile);
                            }
                            if ((strFile.ToLower().IndexOf(@"\container\") != -1) && (Path.GetExtension(strFile.ToLower()) == ".zip"))
                            {
                                if (status)
                                {
                                    HtmlUtils.WriteFeedback(HttpContext.Current.Response, indent, "Installing Container File " + strFile + ":<br>");
                                }
                                SkinController.UploadSkin(Globals.HostMapPath, SkinInfo.RootContainer, Path.GetFileNameWithoutExtension(strFile), strFile);
                                this.DeleteFile(strFile);
                            }
                            if ((strFile.ToLower().IndexOf(@"\language\") != -1) && (Path.GetExtension(strFile.ToLower()) == ".zip"))
                            {
                                if (status)
                                {
                                    HtmlUtils.WriteFeedback(HttpContext.Current.Response, indent, "Installing Language File " + strFile + ":<br>");
                                }
                                new LocaleFilePackReader().Install(strFile);
                                this.DeleteFile(strFile);
                            }
                            if ((strFile.ToLower().IndexOf(@"\template\") != -1) && (strFile.ToLower().IndexOf(".template") != -1))
                            {
                                if (status)
                                {
                                    HtmlUtils.WriteFeedback(HttpContext.Current.Response, indent, "Installing Template " + strFile + ":<br>");
                                }
                                string strNewFile = Globals.HostMapPath + @"\" + Path.GetFileName(strFile);
                                if (File.Exists(strNewFile))
                                {
                                    File.Delete(strNewFile);
                                }
                                File.Move(strFile, strNewFile);
                            }
                            if ((strFile.ToLower().IndexOf(@"\portal\") != -1) && (strFile.ToLower().IndexOf(".resources") != -1))
                            {
                                IEnumerator refObjectHelperL0=null;
                                XmlDocument xmlDoc = new XmlDocument();
                                xmlDoc.Load(strFile);
                                XmlNodeList nodes = xmlDoc.SelectNodes("//dotnetnuke/portals/portal");
                                try
                                {
                                    refObjectHelperL0 = nodes.GetEnumerator();
                                    while (refObjectHelperL0.MoveNext())
                                    {
                                        XmlNode node = (XmlNode) refObjectHelperL0.Current;
                                        if (node != null)
                                        {
                                            if (status)
                                            {
                                                HtmlUtils.WriteFeedback(HttpContext.Current.Response, indent, "Installing Portals:<br>");
                                            }
                                            int intPortalId = DotNetNuke.Services.Upgrade.Upgrade.AddPortal(node, true, indent);
                                            if (intPortalId > -1)
                                            {
                                                HtmlUtils.WriteFeedback(HttpContext.Current.Response, indent + 2, "Successfully Installed Portal " + Conversions.ToString(intPortalId) + ":<br>");
                                            }
                                            else
                                            {
                                                HtmlUtils.WriteFeedback(HttpContext.Current.Response, indent + 2, "Portal failed to install:<br>");
                                            }
                                        }
                                    }
                                }
                                finally
                                {
                                    if (refObjectHelperL0 is IDisposable)
                                    {
                                        (refObjectHelperL0 as IDisposable).Dispose();
                                    }
                                }
                                this.DeleteFile(strFile);
                            }
                        }
                    }
                }
            }
        }

        private void InstallModules(string strFile, bool status, int indent)
        {
            if ((strFile.ToLower().IndexOf(@"\module\") != -1) && (Path.GetExtension(strFile.ToLower()) == ".zip"))
            {
                if (status)
                {
                    HtmlUtils.WriteFeedback(HttpContext.Current.Response, indent, "Installing Module File " + strFile + ":<br>");
                }
                new PaInstaller(strFile, Globals.ApplicationMapPath).Install();
                this.DeleteFile(strFile);
            }
        }
    }
}

